//
//  UIView+YFExtension.m
//  project
//
//  Created by Winny Zhang on 2019/3/22.
//  Copyright © 2019 健健. All rights reserved.
//

#import "UIView+YFExtension.h"

@implementation UIView (YFExtension)

/**
 添加渐变色
 默认 起点位置（0，0），终点位置（0，1）
 垂直渐变，颜色平均分布
 
 @param colors 颜色数组
 */
- (void)setVerticalGradinet:(NSArray<UIColor *> *)colors {
    [self gradinet:colors startPoint:(CGPointMake(0, 0)) endPoint:(CGPointMake(0, 1)) locations:@[]];
}

/**
 添加渐变色
 默认 起点位置（0，0），终点位置（1，0）
 水平渐变，颜色平均分布

 @param colors 颜色数组
 */
- (void)setHorizontalGradinet:(NSArray<UIColor *> *)colors {
    [self gradinet:colors startPoint:(CGPointMake(0, 0)) endPoint:(CGPointMake(1, 0)) locations:@[]];
}
/**
 添加渐变色

 @param colors 渐变颜色数组
 @param startPoint 起始位置，范围为 0～1； 如：CGPointMake(0, 0)表示从 View 定点开始着色渐变
 @param endPoint 终点位置 ，范围为 0～1；如：CGPointMake(1, 1) 表示 到View的底部终止颜色渐变
 @param locations 渐变色的间隔范围，默认平均分布，范围 0～1；如：@[@0.0, @0.2, @0.5]
 */
- (void)gradinet:(NSArray<UIColor *> *)colors startPoint:(CGPoint)startPoint endPoint:(CGPoint)endPoint locations:(NSArray<NSNumber *> *)locations {
    CAGradientLayer *gradient = [CAGradientLayer layer];
    gradient.frame = self.bounds;
    NSMutableArray *cgColors = [NSMutableArray arrayWithCapacity:colors.count];
    [colors enumerateObjectsUsingBlock:^(UIColor * _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
        [cgColors addObject: (id)obj.CGColor];
    }];
    gradient.colors = cgColors;
    gradient.startPoint = startPoint;
    gradient.endPoint = endPoint;
    if (locations.count > 0) {
        gradient.locations = locations;
    }
    [self.layer insertSublayer:gradient atIndex:0];
}

/**
 为 View 设置圆角
 
 @param corner 那些圆角
 @param radius 弧度
 */
- (void)rounded:(UIRectCorner)corner radius: (CGFloat)radius {
    __weak typeof (self) weakSelf = self;
    
    dispatch_async(dispatch_get_main_queue(), ^{
        //圆角 Layer
        UIBezierPath *maskPath =
        [UIBezierPath bezierPathWithRoundedRect: weakSelf.bounds byRoundingCorners:corner cornerRadii:CGSizeMake(radius, radius)];
        CAShapeLayer *maskLayer = [[CAShapeLayer alloc] init];
        maskLayer.frame = weakSelf.bounds;
        maskLayer.path = maskPath.CGPath;
        weakSelf.layer.mask = maskLayer;
    });
}

/**
 为View 设置圆角与边框
 
 @param corner 哪些角设置圆角
 @param radius 圆角弧度
 @param color 边框颜色
 @param width 边框宽度
 */
- (void)rounded:(UIRectCorner)corner radius:(CGFloat)radius borderColor:(UIColor *)color borderWidth:(CGFloat)width {
    __weak typeof (self) weakSelf = self;
    dispatch_async(dispatch_get_main_queue(), ^{
        //圆角 Layer
        UIBezierPath *maskPath =
        [UIBezierPath bezierPathWithRoundedRect: weakSelf.bounds byRoundingCorners:corner cornerRadii:CGSizeMake(radius, radius)];
        CAShapeLayer *maskLayer = [[CAShapeLayer alloc] init];
        maskLayer.path = maskPath.CGPath;
        weakSelf.layer.mask = maskLayer;
        
        // 边框 layer
        UIBezierPath *borderPath =
        [UIBezierPath bezierPathWithRoundedRect: weakSelf.bounds byRoundingCorners:corner cornerRadii:CGSizeMake(radius, radius)];
        CAShapeLayer *borderLayer = [[CAShapeLayer alloc] initWithLayer:maskLayer];
        borderLayer.path = borderPath.CGPath;
        borderLayer.fillColor = [UIColor clearColor].CGColor;
        borderLayer.strokeColor = color.CGColor;
        borderPath.lineWidth = width;
        [self.layer addSublayer:borderLayer];
    });
}

/**
 从 XIB 中加载 View

 @param cls XIB 名称
 @return View
 */
+ (instancetype)loadFromXib:(Class)cls {
    UINib *nib = [UINib nibWithNibName:NSStringFromClass(cls) bundle:nil];
    return [nib instantiateWithOwner:nil options:nil].firstObject;
}


- (void)setupShadowWithView:(UIView *)view {
    dispatch_async(dispatch_get_main_queue(), ^{
        CALayer *layer = [view layer];
        layer.shadowColor = [UIColor colorWithRed:0/255.0 green:0/255.0 blue:0/255.0 alpha:0.06].CGColor;
        layer.shadowOffset = CGSizeMake(0,2);
        layer.shadowOpacity = 1;
        layer.shadowRadius = 4;
        layer.cornerRadius = 6;
        UIBezierPath  *path = [UIBezierPath bezierPathWithRect:view.bounds];
        layer.shadowPath = path.CGPath;
    });
}
@end
